<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\member\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\member\model\MemberModel;

/**
 * Class AdminIndexController
 * @package app\member\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'member/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $where   = [];
        $request = input('request.');

        if (!empty($request['uid'])) {
            $where['id'] = intval($request['uid']);
        }
		
		// $where['id'] = ['neq', "2"]; //去除超管的显示
		
        $keywordComplex = [];
        if (!empty($request['keyword'])) {
            $keyword = $request['keyword'];

            $keywordComplex['user_login|user_nickname|mobile']    = ['like', "%$keyword%"];
        }
        $usersQuery = Db::name('member');

        $join = [
            ['wechat_department_user u','m.openid = u.user_id','LEFT'],
            ['wechat_department d','d.id = u.department_id','LEFT'],
            ['wechat_tag_user t','m.id = t.userid','LEFT'],
            ['wechat_tag wt','t.tagid = wt.tagid','LEFT'],
        ];

        $list = $usersQuery->alias('m')->field('m.*,d.name as department,wt.tagname')->join($join)->whereOr($keywordComplex)->where($where)->order("id ASC,create_time DESC")->paginate(50);
        
        // echo $usersQuery->getLastSql();die;
        // pre($list->toArray());die;
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 编辑会员页面
     */
    public function edit(){

        $id = $this->request->param('id', 0, 'intval');

        //会员信息
        $usersQuery = Db::name('member');
        $post            = $usersQuery->where('id', $id)->find();

        //所属角色
        $post['tagname'] = Db::name('wechat_tag')->alias('t')->join([['wechat_tag_user u','u.tagid = t.tagid','LEFT']])->where('u.userid='.$post['id'])->value('t.tagname');
        //所属部门
        $post['department'] = Db::name('wechat_department')->alias('d')->join([['wechat_department_user u','u.department_id = d.id','LEFT']])->where('u.user_id='.$post['openid'])->value('d.name');
    
        // pre($post);die;
        $this->assign('post', $post);                               //会员信息

        return $this->fetch();

    }

    /**
     * 编辑会员提交
     */
    public function editPost(){

        if ($this->request->isPost()) {

            $data   = $this->request->param();
            $post   = $data['post'];


            $MemberModel = new MemberModel();
            $MemberModel->editData($data['post']);

            //操作日志
            admin_log($post['user_login'], 'edit', 'member');

            $this->success('保存成功!');

                        
            pre($data);
        }

    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("member")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("member")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }
}
